package com.example.wxminiprogram.controller;

import com.example.wxminiprogram.pojo.Goods;
import com.example.wxminiprogram.service.GoodsService;
import jakarta.websocket.server.PathParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.wxminiprogram.pojo.Result;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

@RestController
@CrossOrigin
public class GoodsController {
    @Autowired
    GoodsService goodsService;
    @GetMapping("/api/public/v1/goods/detail")
    public Result getById(@PathParam("id") Integer id){
        return Result.success(this.goodsService.query(id));
    }
    @GetMapping("/api/public/v1/goods/to_shop")
    public Result listToShop(@PathParam("shop_id") Integer shop_id){
        return Result.success(this.goodsService.listToShop(shop_id));
    }
    @PutMapping("/api/public/v1/goods/modify")
    public Result modifyGoods(@RequestBody() Goods curGoods){
        return Result.success(this.goodsService.modifyGoods(curGoods));
    }
    @PostMapping("/api/public/v1/goods/icon")
    public Result uploadIcon(@RequestBody() Object any){
        return Result.success(any);
    }
    @PostMapping("/api/public/v1/goods")
    public Result insert(@RequestBody() Goods goods){
        this.goodsService.insert(goods);
        return Result.success(goods);
    }
    @GetMapping("/api/public/v1/goods/search")
    public Result search(@PathParam("cid") Integer cid,@PathParam("query") String query,@PathParam("pagenum")Integer pagenum,@PathParam("pagesize")Integer pagesize){
        if(query==null && cid == null){
            return Result.error("查询字符串为空");
        }
        return Result.success(this.goodsService.search(cid, query,pagenum,pagesize));
    }
    @PostMapping("/upload/icon")
    public Result uploadIcon(MultipartFile multipartFile,Integer id){
        this.goodsService.uploadIcon(multipartFile,id);
        return Result.success("OK");
    }
    @PostMapping("/upload/introduce")
    public Result uploadIntroduce(MultipartFile multipartFile,Integer id){

        this.goodsService.uploadIntroduce(multipartFile,id);
        return Result.success("OK");
    }
    @PostMapping("/upload/swiper")
    public Result uploadSwiper(MultipartFile multipartFile,Integer id){
        this.goodsService.uploadSwiper(multipartFile,id);
        return Result.success("OK");
    }
    @GetMapping("/api/public/v1/goods/delete")
    public Result deleteGoodsById(@PathParam("id") Integer id){
        this.goodsService.deleteGoodsById(id);
        return Result.success("ok");
    }
    @PutMapping("/api/public/v1/goods/updateQualityById")
    public Result updateQualityById(@RequestBody() Goods curGoods){
        goodsService.updateQualityById(curGoods);
        return Result.success("ok");
    }
}
